import Vue from 'vue'
import App from './App.vue'
import router from './router'
import './plugins/element.js'
import './assets/css/global.css'
import axios from 'axios'
import {Message} from 'element-ui'

Vue.prototype.$http = axios
axios.defaults.baseURL = 'http://localhost:9000/api/'
axios.interceptors.request.use(config => {
  const token = sessionStorage.getItem('token')
  if (token && config.url !== '/login') {
    config.headers['token'] = token
  }
  return config
})

axios.interceptors.response.use(response => {
  return response.data;
}, error => {
  // 统一处理401错误
  if (error.response){
    if (error.response.status === 401) {
      // 清除token
      sessionStorage.removeItem('token');
      router.push('/login');
    }
    if (error.response.status === 500) {
      Message({
        message: error.response.data.msg,
        type: 'error',
      })
    }
  }
  return Promise.reject(error);
});

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
